package com.chinasoft.cr.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinasoft.cr.mapper.TongzhiMapper;
import com.chinasoft.cr.pojo.Tongzhi;
import com.chinasoft.cr.pojo.Result;
import com.chinasoft.cr.pojo.vo.TongzhiParam;
import com.chinasoft.cr.service.TongzhiService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

/**
 * <p>
 * 通知管理前端控制器
 * </p>
 *
 * @author 你的名字
 * @since 2025-01-18
 */
@RestController
@CrossOrigin  // 解决跨域问题
@RequestMapping("/api/tongzhi")
@Tag(name = "通知管理", description = "用于管理通知信息")
public class TongzhiController {

    @Autowired
    private TongzhiService tongzhiService;
    @Autowired
    private TongzhiMapper tongzhiMapper;

    /**
     * 查询通知列表
     *
     * @param pageNum 当前页码
     * @param pageSize 每页大小
     * @return 分页查询结果
     */
    @GetMapping(value = "/list")
    @Operation(summary = "分页获取所有通知信息", description = "分页查询所有通知的详细信息")
    @ApiResponse(responseCode = "200", description = "分页获取所有通知信息成功")
    public Result list(@RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        Page<Tongzhi> aPage = tongzhiService.page(new Page<>(pageNum, pageSize));
        return new Result(200, "查询成功！", aPage);
    }

    /**
     * 根据ID获取通知详情
     *
     * @param id 通知ID
     * @return 通知详情
     */
    @GetMapping(value = "/{id}")
    @Operation(summary = "根据通知ID获取通知信息", description = "根据通知ID查询通知的详细信息")
    @ApiResponse(responseCode = "200", description = "获取通知信息成功")
    @ApiResponse(responseCode = "404", description = "获取通知信息失败,该通知不存在")
    public Result getById(@PathVariable("id") String id) {
        Tongzhi tongzhi = tongzhiService.getById(id);
        if (tongzhi == null) {
            return new Result(404, "通知不存在！", null);
        }
        return new Result(200, "查询成功！", tongzhi);
    }

    /**
     * 根据标题模糊查询通知
     *
     * @param title 包含的关键字
     * @param pageNum 当前页码
     * @param pageSize 每页大小
     * @return 查询结果
     */
    @GetMapping(value = "/search")
    @Operation(summary = "根据标题模糊查询通知", description = "根据通知标题模糊查询通知信息")
    @ApiResponse(responseCode = "200", description = "查询成功")
    public Result search(@RequestParam(required = false) String title,
                         @RequestParam(required = false, defaultValue = "0") int role,
                         @RequestParam(required = false, defaultValue = "0") int userId,
                         @RequestParam(required = false) Integer pageNum,
                         @RequestParam(required = false) Integer pageSize) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        // 构建查询条件
        TongzhiParam tongzhi = new TongzhiParam();
        tongzhi.setTitle(title);
        tongzhi.setRole(role);
        tongzhi.setUserId(userId);
        Page<Tongzhi> page = new Page<>(pageNum, pageSize);
        Page<Tongzhi> resultPage = tongzhiMapper.selectPageVo(page, tongzhi);
        return new Result(200, "查询成功！", resultPage);
    }
    /**
     * 创建通知
     *
     * @param params 通知对象
     * @return 创建结果
     */
    @PostMapping(value = "/create")
    @Operation(summary = "创建通知", description = "创建新通知")
    @ApiResponse(responseCode = "200", description = "创建通知成功")
    @ApiResponse(responseCode = "404", description = "创建通知失败,该通知已存在")
    public Result create(@RequestBody Tongzhi params) {
        try {
            Boolean flag = tongzhiService.save(params);
            if (flag == null) {
                return new Result(500, "增加失败，未知错误！", null);
            }
            if (flag) {
                return new Result(200, "增加成功！", null);
            } else {
                return new Result(404, "增加失败，通知已存在！", null);
            }
        } catch (Exception e) {
            return new Result(500, "增加失败，系统错误：" + e.getMessage(), null);
        }
    }

    /**
     * 根据ID删除通知
     *
     * @param id 通知ID
     * @return 删除结果
     */
    @DeleteMapping(value = "/delete/{id}")
    @Operation(summary = "删除通知", description = "根据通知ID删除通知")
    @ApiResponse(responseCode = "200", description = "删除通知成功")
    @ApiResponse(responseCode = "404", description = "删除通知失败,该通知不存在")
    public Result delete(@PathVariable("id") String id) {
        try {
            Boolean flag = tongzhiService.removeById(id);
            if (flag == null) {
                return new Result(500, "删除失败，未知错误！", null);
            }
            if (flag) {
                return new Result(200, "删除成功！", null);
            } else {
                return new Result(404, "删除失败，通知不存在！", null);
            }
        } catch (Exception e) {
            return new Result(500, "删除失败，系统错误：" + e.getMessage(), null);
        }
    }

    /**
     * 更新通知
     *
     * @param params 通知对象
     * @return 更新结果
     */
//    @PutMapping(value = "/update")
//    @Operation(summary = "更新通知信息", description = "根据通知ID更新通知的信息")
//    @ApiResponse(responseCode = "200", description = "更新通知信息成功")
//    @ApiResponse(responseCode = "404", description = "更新通知信息失败,该通知不存在")
//    public Result update(@RequestBody Tongzhi params) {
//        try {
//            Boolean flag = tongzhiService.updateById(params);
//            if (flag == null) {
//                return new Result(500, "修改失败，未知错误！", null);
//            }
//            if (flag) {
//                return new Result(200, "修改成功！", null);
//            } else {
//                return new Result(404, "修改失败，通知不存在！", null);
//            }
//        } catch (Exception e) {
//            return new Result(500, "修改失败，系统错误：" + e.getMessage(), null);
//        }
//    }
}